package com.losdun.product.controller.app;

import com.alibaba.fastjson2.JSONObject;
import com.losdun.common.annotation.Anonymous;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.product.controller.req.CalPriceReq;
import com.losdun.product.domain.ProdPrice;
import com.losdun.product.domain.Product;
import com.losdun.product.domain.vo.AttributePartsVo;
import com.losdun.product.domain.vo.ProductPriceVo;
import com.losdun.product.manage.ProductPriceCalManage;
import com.losdun.product.service.AttributePartsService;
import com.losdun.product.service.IProductService;
import com.losdun.product.service.ProdPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品Controller
 * 
 * @author sword.ye
 * @date 2025-02-27
 */
@Slf4j
@RestController
@RequestMapping("/app/product/product")
public class AppProductController extends BaseController
{
    @Autowired
    private IProductService productService;
    @Autowired
    private AttributePartsService attributePartsService;
    @Autowired
    private ProductPriceCalManage productPriceCalManage;
    @Autowired
    private ProdPriceService prodPriceService;

    /**
     * 查询产品列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Product product)
    {
        startPage();
        List<Product> list = productService.selectProductList(product);
        return getDataTable(list);
    }
    /**
     * 获取产品详细信息
     */
    @Anonymous
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productService.selectProductById(id));
    }

    /**
     * 获取产品配件列表
     */
    @Anonymous
    @GetMapping(value = "/getAttrParts")
    public AjaxResult getAttributeParts(@RequestParam("attrId") Long attrId)
    {
        List<AttributePartsVo> list = attributePartsService.getAttributeParts(attrId);
        return success(list);
    }

    @Anonymous
    @PostMapping(value = "/calProdPrice")
    public AjaxResult calProdPrice(@RequestBody CalPriceReq req)
    {
        try {
            ProductPriceVo priceVo = productPriceCalManage.calProductPrice(req);
            return success(priceVo);
        } catch (Exception e) {
            log.warn("获取报价错误,err:{}",e);
            return error(e.getMessage());
        }
    }

    @Anonymous
    @PostMapping(value = "/saveProdPrice")
    public AjaxResult saveProdPrice(@RequestBody CalPriceReq req) {
        try {
            Long id = productPriceCalManage.saveProductPrice(req);
            return success(id);
        } catch (Exception e) {
            log.warn("生成报价单错误,err:{}",e);
            return error(e.getMessage());
        }
    }

    @Anonymous
    @GetMapping(value = "/getProdPrice")
    public AjaxResult getProdPrice(@RequestParam Long id){
        ProdPrice prodPrice = prodPriceService.getById(id);
        return success(JSONObject.parseObject(prodPrice.getContent(), ProductPriceVo.class));
    }


}
